package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithSetmealDto(setmealDto);

        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page> selectByPage(int page,int pageSize,String name){
        Page<Setmeal> page1=new Page(page,pageSize);
        Page<SetmealDto> setmealDtoPage=new Page<>();
        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        Page page2 = setmealService.page(page1, wrapper);
        //把获取到的数据拷贝到Setmealde 增强类     并排除掉records集合属性
        BeanUtils.copyProperties(page1,setmealDtoPage,"records");
        //对records重写并装回去
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> list=records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            setmealDto.setCategoryName(byId.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        setmealService.deleteWithDish(ids);
        return R.success("套餐删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status,@RequestParam List<Long> ids){
        //获取所有id，根据id修改status
        LambdaUpdateWrapper<Setmeal> wrapper=new LambdaUpdateWrapper<>();
        wrapper.set(Setmeal::getStatus,status);
        wrapper.in(Setmeal::getId,ids);
        setmealService.update(wrapper);
        return R.success("操作成功");
    }
    @GetMapping("/{id}")
    public R<SetmealDto> seleteDtoById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.seleteSetmealDtoWithDish(id);

        return R.success(setmealDto);
    }
    @PutMapping
    public R<String> updateDtoById(@RequestBody SetmealDto setmealDto){
        setmealService.updateDtoByIdWithDish(setmealDto);

        return R.success("修改套餐成功");
    }
}
